package org.lilred.nzbberry.models;

import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;

public class ConfigModel {
	private static ConfigModel INSTANCE;

	public static ConfigModel getConfig() {
		if (INSTANCE == null) INSTANCE = new ConfigModel();
		return INSTANCE;
	}
	
	private ConfigModel() {
		if (persistentObject.getContents() == null) {
			persistentObject.setContents(config);
		}
		else config = (Hashtable) persistentObject.getContents();
	}
	
	private Hashtable config = new Hashtable();
	private Hashtable defaults = new Hashtable() {
		{ 
			put(SABNZBD_ADDRESS, "http://");
			put(SABNZBD_API_KEY, "");
			put(NZBS_ORG_STRING, "");
			put(REFRESH_CURRENT_INTERVAL, new Long(10000));
			put(REFRESH_ALL_INTERVAL, new Long(60000));
			put(SHOW_HELP, new Boolean(true));
			put(BROWSER_FIELD_COOKIES, new Vector());
		}
	};
	
	private static final long NZBBERRY_SETTINGS_KEY = 0xe7b17a767bbf15e7L;
	private static PersistentObject persistentObject = PersistentStore.getPersistentObject(NZBBERRY_SETTINGS_KEY);
	
	public static final String SABNZBD_ADDRESS = "SABNZBD_ADDRESS";
	public static final String SABNZBD_API_KEY = "SABNZBD_API_KEY";
	public static final String NZBS_ORG_STRING = "NZBS_ORG_STRING";
	public static final String REFRESH_CURRENT_INTERVAL = "REFRESH_CURRENT_INTERVAL";
	public static final String REFRESH_ALL_INTERVAL = "REFRESH_ALL_INTERVAL";
	public static final String SHOW_HELP = "SHOW_HELP";
	public static final String BROWSER_FIELD_COOKIES = "BROWSER_FIELD_COOKIES";
	
	public static final int COOKIE_HOST = 0;
	public static final int COOKIE_URL = 1;
	public static final int COOKIE_TEXT = 2;
	
	private Object get(String key) {
		Object value = config.get(key);
		if (value == null) value = defaults.get(key);
		return value;
	}
	
	private void put(String key, Object value) {
		config.put(key, value);
		persistentObject.commit();
	}
	
	public void setSabnzbdAddress(String address) { put(SABNZBD_ADDRESS, address); }
	public String getSabnzbdAddress() { return (String) get(SABNZBD_ADDRESS); }
	public void setSabnzbdApiKey(String apiKey) { put(SABNZBD_API_KEY, apiKey); }
	public String getSabnzbdApiKey() { return (String) get(SABNZBD_API_KEY); }
	public void setNzbsOrgString(String nzbsOrgString) { put(NZBS_ORG_STRING, nzbsOrgString); }
	public String getNzbsOrgString() { return (String) get(NZBS_ORG_STRING); }
	public void setRefreshCurrentInterval(long refreshCurrentInterval) { put(REFRESH_CURRENT_INTERVAL, new Long(refreshCurrentInterval)); }
	public long getRefreshCurrentInterval() { return ((Long) get(REFRESH_CURRENT_INTERVAL)).longValue(); }
	public void setRefreshAllInterval(long refreshAllInterval) { put(REFRESH_ALL_INTERVAL, new Long(refreshAllInterval)); }
	public long getRefreshAllInterval() { return ((Long) get(REFRESH_ALL_INTERVAL)).longValue(); }
	public void setShowHelp(boolean showHelp) { put(SHOW_HELP, new Boolean(showHelp)); }
	public boolean getShowHelp() { return ((Boolean) get(SHOW_HELP)).booleanValue(); }
	public void addCookie(String[] cookie) {
		//Get the name of the cookie - the text up to the first equal sign
		String cookieText = cookie[COOKIE_TEXT];
		int equalsIndex = cookieText.indexOf('=');
		String newCookieName = cookieText.substring(0, equalsIndex);
		Vector allCookies = ((Vector) get(BROWSER_FIELD_COOKIES));
		boolean cookieReplaced = false;
		for (int i = 0; i < allCookies.size(); i++) {
			String[] existingCookie = (String[]) allCookies.elementAt(i);
			if (existingCookie[COOKIE_TEXT].startsWith(newCookieName)) {
				allCookies.removeElementAt(i);
				allCookies.insertElementAt(cookie, i);
				cookieReplaced = true;
				break;
			}
		}
		if (!cookieReplaced) allCookies.addElement(cookie);
		put(BROWSER_FIELD_COOKIES, allCookies);
	}
	public void clearCookies() { config.put(BROWSER_FIELD_COOKIES, new Vector()); }
	public Vector getCookies(String domain) {
		Vector allCookies = (Vector) get(BROWSER_FIELD_COOKIES);
		Vector domainCookies = new Vector();
		for (int i = 0; i < allCookies.size(); i++) {
			String[] cookie = (String[]) allCookies.elementAt(i);
			if (cookie[COOKIE_HOST].equals(domain)) domainCookies.addElement(cookie);
		}
		return domainCookies;
	}
	
	public void reset() {
		config = new Hashtable();
	}
}
